<template>
    <div>
        <h1>{{id?`编辑`:`新建`}}寻人</h1>
        <el-form label-width='120px'>
            <el-form-item label="名称"> 
                <el-input v-model="model.name"></el-input>
            </el-form-item>
            <el-form-item label="走失时间"> 
                <el-input v-model="model.time"></el-input>
            </el-form-item>
            <el-form-item label="性别"> 
                <el-input v-model="model.sex"></el-input>
            </el-form-item>
            <el-form-item label="身高"> 
                <el-input v-model="model.height"></el-input>
            </el-form-item>
            <el-form-item label="出生年月"> 
                <el-input v-model="model.birth"></el-input>
            </el-form-item>
            <el-form-item label="失踪地点"> 
                <el-input v-model="model.szdidian"></el-input>
            </el-form-item>
            <el-form-item label="失踪描述"> 
                <el-input type="textarea" v-model="model.content"></el-input>
            </el-form-item>
            <el-form-item label="所属标签"> 
                <el-input v-model="model.label"></el-input>
            </el-form-item>
            <el-form-item label="发布人"> 
                <el-input v-model="model.postman"></el-input>
            </el-form-item>
            <el-form-item label="联系电话"> 
                <el-input v-model="model.phone"></el-input>
            </el-form-item>
            <!-- <el-form-item label='广告'> -->               
            <el-form-item label='图片' style="margin-top:0.5rem">
                <el-upload 
                class="avatar-uploader"
                :action="uploadURL"
                :headers="getAuthHeaders()"
                :show-file-list="false"
                :on-success="res=>$set(model,'img',res.url)"
                >
                    <!-- 有图片显示图片，没有则显示上传图标,:src显示的图片 -->
                    <img v-if="model.img" :src="model.img" class="avatar"/>
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            </el-form-item>
            <el-form-item>
                <el-button type='primary' @click="save()" native-type='submit'>
                    保存
                </el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    props:{
        id:{}
    },
    data(){
        return{
            model:{
               
            },
        }
    },
    methods:{
        async save(){
            let res
            if(this.id){
                res = await this.$http.put(`rest/FindPerson/${this.id}`,this.model)
            }else{
                res = await this.$http.post('rest/FindPerson',this.model)
            }
            this.$router.push('/findPerson/list')
            this.$message({
                type:'success',
                message:'保存成功'
            })
        },
        async fetch(){
            const res = await this.$http.get('rest/FindPerson/'+this.id)
            this.model = Object.assign({},this.model,res.data)
        },
        
    },
    created(){
         
        if(this.id){
            this.fetch()
        }
    }
}
</script>